package com.lidroid.xutils.sample.menuapp.adaptor;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.lidroid.xutils.bitmap.callback.DefaultBitmapLoadCallBack;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.sample.BitmapHelp;
import com.lidroid.xutils.sample.R;
import com.lidroid.xutils.sample.menuapp.enty.Dish;
import com.lidroid.xutils.sample.menuapp.enty.Step;
import com.lidroid.xutils.view.annotation.ViewInject;

public class MenusAdaptor extends BaseAdapter {
 
	private  Context mContext;
	private ArrayList<Dish> menuList;
      private  LayoutInflater mInflater;
	  public MenusAdaptor(Context mContext) {
		super();
		
		this.mContext = mContext;
		this.mInflater = LayoutInflater.from(mContext) ;
	}
		public void BindData(ArrayList<Dish> data){
			
			this.menuList = data;
			
			System.out.println("menuList"+menuList.size());
		}
 


	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return menuList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return menuList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			  
			convertView = mInflater.inflate(
					R.layout.activity_menu_list_item, null);
			holder.albumsIV = (ImageView) convertView
					.findViewById(R.id.albums_iv);
			holder.titleTV = (TextView) convertView
					.findViewById(R.id.title_tv);
			holder.tagsTV = (TextView) convertView
					.findViewById(R.id.tags);

			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
//		holder.albumsIV.setImageBitmap(menList.get(position)
//				.getAlbums());
		
		Dish dishData =menuList.get(position);
		holder.titleTV.setText(dishData.getName());
	 System.out.println("测试数据"+dishData.getName());
		holder.tagsTV.setText(dishData.getTags());
		//   holder.imgPb.setProgress(0);
		Step[] step=dishData.getSteps();
		 BitmapUtils bitmapUtils  = BitmapHelp.getBitmapUtils( mContext);
	        bitmapUtils.configDefaultLoadingImage(R.drawable.ic_launcher);
	        bitmapUtils.configDefaultLoadFailedImage(R.drawable.bitmap);
	        bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);
	        bitmapUtils.display(holder.albumsIV, dishData.getImg());
	    	
//           bitmapUtils.display(holder.albumsIV, , new CustomBitmapLoadCallBack(holder));
		return convertView;
	}
	private class ViewHolder {
		ImageView albumsIV;
		TextView titleTV;
		TextView tagsTV;
	}
	
	public void LoadImageFromURL(String url) {
		new HttpUtils().send(HttpRequest.HttpMethod.GET, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                           
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                    }
                });
	}
	
	
	
	
    public class CustomBitmapLoadCallBack extends DefaultBitmapLoadCallBack<ImageView> {
        private final ImageItemHolder holder;

        public CustomBitmapLoadCallBack(ImageItemHolder holder) {
            this.holder = holder;
        }

        @Override
        public void onLoading(ImageView container, String uri, BitmapDisplayConfig config, long total, long current) {
            this.holder.imgPb.setProgress((int) (current * 100 / total));
        }

        @Override
        public void onLoadCompleted(ImageView container, String uri, Bitmap bitmap, BitmapDisplayConfig config, BitmapLoadFrom from) {
            //super.onLoadCompleted(container, uri, bitmap, config, from);
            fadeInDisplay(container, bitmap);
            this.holder.imgPb.setProgress(100);
        }
    }
    private class ImageItemHolder {
        @ViewInject(R.id.img_item)
        private ImageView imgItem;

        @ViewInject(R.id.img_pb)
        private ProgressBar imgPb;
    }
    
    private static final ColorDrawable TRANSPARENT_DRAWABLE = new ColorDrawable(android.R.color.transparent);
    private void fadeInDisplay(ImageView imageView, Bitmap bitmap) {
        final TransitionDrawable transitionDrawable =
                new TransitionDrawable(new Drawable[]{
                        TRANSPARENT_DRAWABLE,
                        new BitmapDrawable(imageView.getResources(), bitmap)
                });
        imageView.setImageDrawable(transitionDrawable);
        transitionDrawable.startTransition(500);
    }
		
	}
	


